<?php

/**
* alltosun.com 优惠券widget coupon.php
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 吴松键 (wusj@alltosun.com) $
* $Date: 2014-9-4 上午10:55:39 $
* $Id$
*/

class coupon_widget
{
    private $per_page = 10;
    private $page_no  = 1;

    /**
     * 获取已拥有优惠券列表
     */
    public function get_new_list($params = array())
    {
        //$filter['status'] = Request::getParam('status', 0);
        $this->page_no = Request::Get('page_no', 1);
        $user_id = user_helper::get_user_id();

        if (!$user_id) {
            return array();
        }

        if (isset($params['per_page']) && $params['per_page']) {
            $this->per_page = $params['per_page'];
        }
        if (isset($params['page_no']) && $params['page_no']) {
            $this->page_no = $params['page_no'];
        }
        if (isset($params['user_id']) && $params['user_id']) {
            $filter['user_id'] = $params['user_id'];
        } else {
            $filter['user_id'] = $user_id;
        }
        if (isset($params['status']) && $params['status']) {
            $filter['status'] = $params['status'];
            Response::assign('status', $params['status']);

            if ($params['status'] < 3) {
                unset($filter['status']);
                $filter['`status` <'] = 3;
            }
        }
        if (!$filter['status']) {
            unset($filter['status']);
        }
        if ($params['status'] < coupon_config::COUPON_OVERDUE) {
            $filter['coupon_id'] = coupon_helper::get_coupon_ids('status');
            $filter['`coupon_id` <>'] = coupon_helper::get_coupon_ids('end_time');
        }


        // 获取已过期优惠券
        if ($params['status'] == coupon_config::COUPON_OVERDUE) {
            unset($filter['status']);
            $filter['coupon_id'] = coupon_helper::get_coupon_ids('end_time');
        }

        $list = array();
        $count = _model('coupon_relation')->getTotal($filter);
        if ($count) {
            $pager = new Pager($this->per_page);
            if ($pager->generate($count)) {
                Response::assign('pager', $pager);
            }

            $list = _model('coupon_relation')->getList($filter, ' ORDER BY `offer_price` ASC,`add_time` DESC '.$pager->getLimit($this->page_no));
            foreach ($list as $k=>$v) {
                $coupon_info = _uri('coupon', $v['coupon_id']);
                $list[$k]['title']       = $coupon_info['title'];
                $list[$k]['start_time']  = $coupon_info['start_time'];
                $list[$k]['end_time']    = $coupon_info['end_time'];
                $list[$k]['category_id']    = $coupon_info['category_id'];
            }
        }

        return $list;
    }

    /**
     * 获取已拥有优惠券列表
     */
    public function get_list($params = array())
    {
        $filter['status'] = Request::getParam('status', 0);
        $this->page_no = Request::Get('page_no', 1);
        $user_id = user_helper::get_user_id();

        if (!$user_id) {
            return array();
        }

        if (isset($params['per_page']) && $params['per_page']) {
            $this->per_page = $params['per_page'];
        }
        if (isset($params['page_no']) && $params['page_no']) {
            $this->page_no = $params['page_no'];
        }
        if (isset($params['user_id']) && $params['user_id']) {
            $filter['user_id'] = $params['user_id'];
        } else {
            $filter['user_id'] = $this->user_id;
        }
        if (isset($params['status']) && $params['status']) {
            $filter['status'] = $params['status'];
        }
        if (!$filter['status']) {
            unset($filter['status']);
        }

        // 获取已过期优惠券
        if ($params['status'] == coupon_config::COUPON_OVERDUE) {
            unset($filter['status']);
        }

        $list = array();
        $count = _model('coupon_relation')->getTotal($filter);
        if ($count) {
            $pager = new Pager($this->per_page);
            if ($pager->generate($count)) {
                Response::assign('pager', $pager);
            }

            $list = _model('coupon_relation')->getList($filter, ' ORDER BY `status` ASC,`add_time` DESC '.$pager->getLimit($this->page_no));
            foreach ($list as $k=>$v) {
                $coupon_info = _uri('coupon', $v['coupon_id']);
                // $curr_status = coupon_helper::get_is_valid($v['coupon_id']);
                if ($coupon_info['status']) {
                    $list[$k]['title']       = $coupon_info['title'];
                    $list[$k]['offer_price'] = $coupon_info['offer_price'];
                    $list[$k]['start_time']  = $coupon_info['start_time'];
                    $list[$k]['end_time']    = $coupon_info['end_time'];
                    /*if ($v['status'] == coupon_config::COUPON_USE) {
                        $curr_status = 1;
                    }
                    if (($curr_status && $params['status'] && $params['status'] == coupon_config::COUPON_OVERDUE) ||
                        (!$curr_status && $params['status'] && $params['status'] != coupon_config::COUPON_OVERDUE)) {
                        unset($list[$k]);
                    }*/
                } else {
                    unset($list[$k]);
                }
            }

            $tmp = '';
            foreach ($list as $k=>$v) {
                if (coupon_helper::get_is_valid($v['coupon_id']) == 0) {
                    $tmp = $v;
                    unset($list[$k]);
                    array_push($list, $tmp);
                }
            }

            // 重建索引
            $list = array_values($list);
        }

        return $list;
    }
}
?>